// Generated by CoffeeScript 2.5.1
var app;

import {
  createApp
} from '/scripts/vue.esm-browser.prod.min.js';

app = createApp({
  data: () => {
    return {
      formal: false,
      gender: 'm',
      history: [],
      initialScene: null,
      name: '',
      reader: new FileReader(),
      scene: null,
      showHistory: false,
      swtches: new Map()
    };
  },
  methods: {
    parseSwtches: function(text) {
      var match, swtches;
      swtches = (function() {
        var results;
        results = [];
        while (match = text.match(/^([+-])([\p{L}\d_]+) (.*)/u)) {
          text = match[3];
          results.push({
            value: match[1] === '+',
            name: match[2]
          });
        }
        return results;
      })();
      return {text, swtches};
    },
    loadStory: function(story, version = 0) {
      var paragraphs, scenes;
      paragraphs = story.split('\n\n');
      scenes = new Map(paragraphs.map((paragraph) => {
        var description, options, scene, title;
        [title, description, ...options] = paragraph.split('\n').filter((line) => {
          return line;
        });
        scene = {
          title: title,
          description: version >= 1 ? this.parseSwtches(description) : {
            text: description,
            swtches: []
          },
          options: options.map((option) => {
            var consequences, left, next, prerequisites, right, text;
            [left, right] = option.split(' > ');
            ({
              text,
              swtches: prerequisites
            } = this.parseSwtches(left));
            ({
              text: next,
              swtches: consequences
            } = this.parseSwtches(right));
            return {prerequisites, text, next, consequences};
          })
        };
        return [title, scene];
      }));
      scenes.forEach((scene) => {
        return scene.options.forEach((option) => {
          option.next = scenes.get(option.next);
          if (option.next == null) {
            throw `Neexistující scéna: ${option.next}`;
          }
        });
      });
      return this.initialScene = scenes.get(story.match(/.*/)[0]);
    },
    loadStoryFromFile: function(file) {
      var _, version;
      [_, this.name, version] = file.name.match(/^(.*)\.iprib(\d+)$/);
      version = parseInt(version);
      this.reader.onload = (event) => {
        return this.loadStory(event.target.result, version);
      };
      return this.reader.readAsText(file);
    },
    triggerSwtches: function(swtches) {
      return swtches.forEach((swtch) => {
        return this.swtches.set(swtch.name, swtch.value);
      });
    },
    enterScene: function(scene) {
      this.triggerSwtches(scene.description.swtches);
      return this.scene = scene;
    },
    optionLegal: function(option) {
      return option.prerequisites.every((swtch) => {
        var ref;
        return swtch.value === ((ref = this.swtches.get(swtch.name)) != null ? ref : false);
      });
    },
    triggerOption: function(option) {
      if (this.showHistory) {
        this.history.push(this.interpolate(this.scene.description.text));
        this.history.push(this.interpolate(option.text));
      }
      this.triggerSwtches(option.consequences);
      return this.enterScene(option.next);
    },
    interpolate: function(text) {
      var last;
      while (true) {
        last = text;
        text = text.replaceAll(/\[([^\/|\]}]*)\/([^\/|\]}]*)\]/g, (_, masculine, feminine) => {
          if (this.gender === 'f') {
            return feminine;
          } else {
            return masculine;
          }
        }).replaceAll(/\[([^\/|\]}]*)\|([^\/|\]}]*)\]/g, (_, informal, formal) => {
          if (this.formal) {
            return formal;
          } else {
            return informal;
          }
        }).replaceAll(/\{([^\/|\]}]*)\|([^\/|\]}]*)\/([^\/|\]}]*)\}/g, (_, condition, ifTrue, ifFalse) => {
          if (this.swtches.get(condition)) {
            return ifTrue;
          } else {
            return ifFalse;
          }
        });
        if (text === last) {
          return text;
        }
      }
    },
    startStory: function() {
      return this.enterScene(this.initialScene);
    },
    endStory: function() {
      if (this.scene.options.length === 0 || confirm("Skutečně chcete příběh ukončit? Smaže se tím všechen postup.")) {
        this.scene = null;
        this.swtches = new Map();
        return this.history = [];
      }
    }
  }
});

window.vm = app.mount('#app');
